package com.nf.health.app.customview;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

/**
 * Created by hepan on 2017/4/28.
 * 汗液前期处理 pop
 */
public class SweatSetPopupWindow extends PopupWindow implements View.OnClickListener {

    TextView tvDeviceNameUp;
    TextView tvBindStateUp;
    LinearLayout llBindDeviceUp;
    TextView tvDeviceNameDown;
    TextView tvBindStateDown;
    LinearLayout llBindDeviceDown;
    TextView tvSweatSet;
    TextView tvHaveNoDevice;
    TextView tvSetFreqUp;
    TextView tvSetFreqDown;
    private View view;
    private String deviceNameUp;
    private String deviceNameDown;
    private String freqUP;
    private String freqDown;

    public SweatSetPopupWindow(Activity act) {
        super(act);
        view = LayoutInflater.from(act).inflate(R.layout.sweat_pop_set_menu, null);
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        this.setBackgroundDrawable(new BitmapDrawable());//必要,保证背景是自定义的.9patch图
        deviceNameUp = PreferenceHelper.getString(PreferenceHelper.SWEAT_NANLYZER_ADDRESS_ONE, "");
        deviceNameDown = PreferenceHelper.getString(PreferenceHelper.SWEAT_NANLYZER_ADDRESS_TWO, "");
        freqUP = PreferenceHelper.getString(PreferenceHelper.SWEAT_NANLYZER_FREQUENCY_ONE, "");
        freqDown = PreferenceHelper.getString(PreferenceHelper.SWEAT_NANLYZER_FREQUENCY_TWO, "");
        initView();
        initDataAndListener();
    }

    private void initView() {
        tvDeviceNameUp = ViewUtil.getView(view, R.id.tv_device_name_up);
        tvBindStateUp = ViewUtil.getView(view, R.id.tv_bind_state_up);
        llBindDeviceUp = ViewUtil.getView(view, R.id.ll_bind_device_up);
        tvDeviceNameDown = ViewUtil.getView(view, R.id.tv_device_name_down);
        tvBindStateDown = ViewUtil.getView(view, R.id.tv_bind_state_down);
        llBindDeviceDown = ViewUtil.getView(view, R.id.ll_bind_device_down);
        tvSweatSet = ViewUtil.getView(view, R.id.tv_sweat_set);
        tvHaveNoDevice = ViewUtil.getView(view, R.id.tv_have_no_device);
        tvSetFreqUp = ViewUtil.getView(view, R.id.tv_set_freq_up);
        tvSetFreqDown = ViewUtil.getView(view, R.id.tv_set_freq_down);

    }

    private void initDataAndListener() {
        if (!TextUtils.isEmpty(deviceNameUp)) {
            tvDeviceNameUp.setText(deviceNameUp);
//            tvBindStateUp.setText("解除绑定");
        } else {
            tvDeviceNameUp.setText("暂未绑定");
//            tvBindStateUp.setText("上身设备");
        }
        if (!TextUtils.isEmpty(deviceNameDown)) {
            tvDeviceNameDown.setText(deviceNameDown);
//            tvDeviceNameDown.setVisibility(View.VISIBLE);
//            tvBindStateDown.setText("解除绑定");
        } else {
            tvDeviceNameDown.setText(deviceNameDown);
//            tvBindStateDown.setText("下身设备");
        }
        if (!TextUtils.isEmpty(freqUP)) {
            tvSetFreqUp.setText("设备(1)采集间隔(" + freqUP + "秒)");
        }
        if (!TextUtils.isEmpty(freqDown)) {
            tvSetFreqDown.setText("设备(2)采集间隔(" + freqDown + "秒)");
        }
        llBindDeviceUp.setOnClickListener(this);
        llBindDeviceDown.setOnClickListener(this);
        tvHaveNoDevice.setOnClickListener(this);
        tvSetFreqUp.setOnClickListener(this);
        tvSetFreqDown.setOnClickListener(this);
    }

    private OnMyClickListener listener;

    public void setOnMenuItemClickListener(OnMyClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
        if (listener != null) {
            listener.onClick(v.getId(), "");
        }
    }
}
